package com.bw.z.mvp.base.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bw.z.mvp.base.presenter.IPresenter;
import com.bw.z.utils.ToastUtils;

import javax.inject.Inject;

public abstract class BaseVpFragment<P extends IPresenter> extends Fragment
        implements IView, IFragment {

    @Inject
    protected P p;

    protected View rootView;//当前fragment根结点视图

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(bindLayout(), container, false);
        initView();
        init();
        return rootView;
    }

    private void init(){
        inject();
        getLifecycle().addObserver(p);
        initData();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getLifecycle().removeObserver(p);
    }

    public <T extends View> T f(int resId) {
        return rootView.findViewById(resId);
    }

    @Override
    public void startPage(Bundle bundle, Class clazz) {
        Intent intent = new Intent(getActivity(), clazz);
        if (bundle != null)
            intent.putExtras(bundle);
        getActivity().startActivity(intent);
    }

    public void startPage(Class clazz) {
        startPage(null, clazz);
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    @Override
    public void showMsg(String msg) {
        ToastUtils.showMsg(msg);
    }

}
